/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.common;

import com.mxg.blog.entity.User;
import com.mxg.blog.service.UserService;

import java.util.Calendar;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/6/25 10:42
 * 用于与用户有关的操作
 */
public class UserOperate {
    // 创建
    private Integer userId;
    private static volatile UserOperate instance;
    User user;
    CqLogger cqLogger = CqLogger.getInstance();
    private UserService userService;

    public void setUserId(Integer userId){
        this.userId = userId;
        try{
            this.user = userService.getById(userId);
        }catch (Exception e){
            cqLogger.print("错误", e);
        }
    }

    private void setUserService(UserService userService){
        this.userService = userService;
    }

    UserOperate(Integer userId){
        this.userId = userId;
    }

    // 创建类
    public static UserOperate getInstance(Integer userId, UserService userService){
        if (instance == null) {
            synchronized (UserOperate.class) {
                if (instance == null) {
                    instance = new UserOperate(userId);
                }
            }
        }
        instance.setUserId(userId);
        instance.setUserService(userService);
        return instance;
    }

    // 获取头像操作
    public String getAvatar(){
        if(user.getAvatar() != null){
            return user.getAvatar();
        }else{
            return "https://q1.qlogo.cn/g?b=qq&nk=1415886740&s=100";
        }
    }

    // 获取email
    public String getEmail(){
        return user.getEmail();
    }

    // 获取电话
    public String getPhone(){
        return user.getPhone();
    }

    // 获取昵称
    public String getNickName(){
        return user.getUserName();
    }
}
